var keystone = require('keystone');
var Promise = require("bluebird");

exports = module.exports = function (req, res) {
  let locals = res.locals;

  locals.data = {
    isLogin: !!locals.user,
    user: locals.user
  };

  if (locals.data.isLogin && (req.path === '/' || req.path === '/index')) {

    return keystone.next_app.render(req, res, '/index', req.query);

  } else if (!locals.data.isLogin) {

    return res.redirect('/login');

  } else {
    return res.json(locals.data);
  }

};
